package tom.client;

import tom.shared.AppUser;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

public class FormLogin extends FlexTable {

	public static AppUser currUser = null;
	public static FormLogin frmLogin = GWT.create(FormLogin.class);

	private boolean connected;

	public boolean isConnected() {
		return connected;
	}

	public String getPassword() {
		return password;
	}

	private String user;

	public void setUser(String user) {
		this.user = user;
	}

	private String password;

	private Button button;
	private TextBox textUser;
	// private Label labelUser;
	private Label labelPasswd;
	private PasswordTextBox textPasswd;

	private ServiceAsync myService = Library.getService();

	private AsyncCallback<AppUser> loginCallback = new AsyncCallback<AppUser>() {
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
			textPasswd.selectAll();
			textPasswd.setFocus(true);
		}

		public void onSuccess(AppUser obj) {
			currUser = obj;
			if (obj != null) {
				user = textUser.getText();
				disableLogin();
				windmill.refresh();
			} else {
				enableLogin();
				textPasswd.selectAll();
				textPasswd.setFocus(true);
				Window.alert("ชื่อผู้ใช้หรือรหัสผ่านผิดพลาด!");
			}
		}
	};

	private AsyncCallback<Void> logoutCallback = new AsyncCallback<Void>() {
		public void onFailure(Throwable caught) {
			Window.alert(caught.getMessage());
		}

		public void onSuccess(Void result) {
			// myService.getSessionVar("user", userCallback);
			currUser = null;
			user = null;
			enableLogin();
			windmill.refresh();
		}
	};

	public void disableLogin() {
		getButton().setText("Logout");
		password = textPasswd.getText();
		// this.setWidget(1, 1, getButton());
		textUser.setText(user);
		// FormLogin.this.setText(4, 2, "[" + user + "]");
		textUser.setEnabled(false);
		textPasswd.setEnabled(false);
		labelPasswd.setVisible(false);
		textPasswd.setVisible(false);
		// button.setText("Logout");
		// // moduleForm.refresh();
		// button.setEnabled(true);
		connected = true;
	}

	public void enableLogin() {
		// this.setWidget(1, 1, labelUser);
		getButton().setText("Login");
		textUser.setEnabled(true);
		labelPasswd.setVisible(true);
		textPasswd.setVisible(true);
		textPasswd.setEnabled(true);
		user = null;
		password = null;
		textPasswd.setText(password);
		// button.setText("Login");
		textUser.selectAll();
		textUser.setFocus(true);
		// this.setText(4, 2, "");
		// button.setEnabled(true);
		// moduleForm.refresh();
		connected = false;
	}

	private WindMill windmill;

	public void setWindmill(WindMill windmill) {
		this.windmill = windmill;
	}

	public FormLogin() {
		this.setBorderWidth(0);
		// labelUser = new Label("User:");
		labelPasswd = new Label("Password:");
		this.setSize("200px", "65px");
		// this.setCellSpacing(5);
		this.setTitle("Login");
		this.setWidget(1, 1, getButton());
		this.setWidget(1, 2, getTextUser());
		this.setWidget(2, 1, labelPasswd);
		this.setWidget(2, 2, getTextPasswd());
		// myService.getSessionVar("user", userCallback);
	}

	public Button getButton() {
		if (button == null) {
			button = new Button("Login");
			button.setStyleName("Button");
			// button.setSize("60px", "22px");
			// button.setStyleName("button");
			button.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					if (FormLogin.this.isConnected())
						logout();
					else
						login();
				}
			});
		}
		return button;
	}

	public void login() {
		textUser.setEnabled(false);
		textPasswd.setEnabled(false);
		textUser.setText(textUser.getText().toLowerCase());
		myService.login(textUser.getText(),
				Library.encode(textPasswd.getText()), loginCallback);
	}

	public void logout() {
		myService.logout(logoutCallback);
	}

	public TextBox getTextUser() {
		if (textUser == null) {
			textUser = new TextBox();
			textUser.setWidth("105px");
			// textUser.setSize("105px", "22px");
			textUser.addKeyDownHandler(new KeyDownHandler() {
				@Override
				public void onKeyDown(KeyDownEvent event) {
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
						textPasswd.selectAll();
						textPasswd.setFocus(true);
					}
				}
			});
		}
		return textUser;
	}

	public PasswordTextBox getTextPasswd() {
		if (textPasswd == null) {
			textPasswd = new PasswordTextBox();
			textPasswd.setWidth("105px");
			// textPasswd.setSize("105px", "22px");
			textPasswd.addKeyDownHandler(new KeyDownHandler() {
				@Override
				public void onKeyDown(KeyDownEvent event) {
					if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
						login();
					}
				}
			});
		}
		return textPasswd;
	}

	public String getUser() {
		return user;
	}

}
